package com.project.generator.utils;

import lombok.extern.slf4j.Slf4j;

/**
 * @program: generator
 * @description: 数据库链接拼接
 * @Author YM
 * @create: 2022-09-30
 */
@Slf4j
public class DBParamUtils {

    public static String DRIVER_CLASS_NAME_MYSQL = "com.mysql.cj.jdbc.Driver";

    public static String DRIVER_CLASS_NAME_POSTGRESQL = "org.postgresql.Driver";

    public static String DRIVER_CLASS_NAME_ORACLE = "oracle.jdbc.driver.OracleDriver";

    public static String DRIVER_CLASS_NAME_SQLSERVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

    public static String DRIVER_CLASS_NAME_SYSBASE = "com.sybase.jdbc.SybDriver";

    public static String DRIVER_CLASS_NAME_DB2 = "com.ibm.db2.jdbc.app.DB2Driver";

    public static String getDriverClassName(String dbType) {

        String driverClassName = null;
        switch (dbType.toUpperCase()) {
            case "MYSQL":
                driverClassName = DRIVER_CLASS_NAME_MYSQL;
                break;
            case "POSTGRESQL":
                driverClassName = DRIVER_CLASS_NAME_POSTGRESQL;
                break;
            case "ORACLE":
                driverClassName = DRIVER_CLASS_NAME_ORACLE;
                break;
            case "SQLSERVER":
                driverClassName = DRIVER_CLASS_NAME_SQLSERVER;
                break;
            case "SYBASE":
                driverClassName = DRIVER_CLASS_NAME_SYSBASE;
                break;
            case "DB2":
                driverClassName = DRIVER_CLASS_NAME_DB2;
                break;
            default:
                break;
        }
        return driverClassName;
    }

    public static String getUrl(String dbType, String ip, String port, String database) {
        String url = null;
        switch (dbType.toUpperCase()) {
            case "MYSQL":
                //jdbc:mysql://localhost/数据库名?[后接参数]
                url = "jdbc:mysql://" + ip + ":" + port + "/" + database + "?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8";
                break;
            case "POSTGRESQL":
                //jdbc:postgresql://172.16.0.6:5432/biaoqian?useSSL=false
                url = "jdbc:postgresql://" + ip + ":" + port + "/" + database + "?useSSL=false";
                break;
            case "ORACLE":
                //jdbc:oracle:thin:@localhost:1521:数据库名
                url = "jdbc:oracle:thin:@" + ip + ":" + port + ":" + database;
                break;
            case "SQLSERVER":
                //jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=数据库名  连接SQLServer 2000
                url = "jdbc:microsoft:sqlserver://" + ip + ":" + port + ";DatabaseName=" + database;
                break;
            case "SYBASE":
                //jdbc:sybase:Tds:localhost:5007/数据库名
                url = "jdbc:sybase:Tds:" + ip + ":" + port + "/" + database;
                break;
            case "DB2":
                //jdbc:db2://localhost:5000/数据库名
                url = "jdbc:db2://" + ip + ":" + port + "/" + database;
                break;
            default:
                break;
        }
        return url;
    }

}
